package io.renren.task.spider;

import com.xiaoleilu.hutool.http.HttpUtil;
import com.xiaoleilu.hutool.io.FileUtil;
import io.renren.entity.SysUserEntity;
import io.renren.service.SysUserService;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Spider;

/**
 * 测试定时任务(演示Demo，可删除)
 * <p>
 * testTask为spring bean的名称
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2016年11月30日 下午1:34:24
 */
@Component("spiderTask")
public class SpiderTask extends BaseSpider{
    private Logger logger = LoggerFactory.getLogger(getClass());


	@Override
	public void process(Page page) {
		Document doc=page.getHtml().getDocument();
		Elements elements=doc.select(".toplist_right ul li a img");
		String fileUrl;
		Element element;
		StringBuffer name;
		for (int i = 0; i < elements.size(); i++) {
			name=new StringBuffer("D:\\output\\");
			element=elements.get(i);
			fileUrl=element.attr("lazysrc");
			System.out.println(element.attr("alt")+":"+fileUrl);
//			name.append(i);
//			name.append(".");
//			name.append(FileUtil.extName(fileUrl));
//			HttpUtil.downloadFile(fileUrl,name.toString());
		}

	}
	public void run(){
		Spider.create(new SpiderTask()).addUrl("http://www.nanameitu.com/top/").thread(5).run();
	}
}
